package com.hsylient.spring.interfaceinspring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * 我应用程序上下文意识到
 *
 * @author Hsylient
 * @date 2022/12/20
 */
@Component
public class MyApplicationContextAware implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public <T> T getBeanByName(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
